package com.zzx.boot.websocket;


import com.zzx.boot.websocket.handler.GlobalWebSocketFrameHandler;
import com.zzx.boot.websocket.handler.MessageHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class WebSocketServer {

    //创建两个线程组
    public WebSocketServer(int port) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();//处理连接请求
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();//处理业务
        WebSocketServer than = this;
        try{
            // 创建服务端启动对象 设置参数
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 设置两个线程组
            serverBootstrap.group(bossGroup, workerGroup)
                    // 设置服务端通道实现类型
                    .channel(NioServerSocketChannel.class)
                    // 设置线程队列得到连接个数
                    .option(ChannelOption.SO_BACKLOG,128)
                    // 设置保持活动连接状态
                    .childOption(ChannelOption.SO_KEEPALIVE,true)
                    // 使用匿名内部类的形式初始化通道对象
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel channel) throws Exception {
                            channel.pipeline().addLast(new HttpServerCodec());// 使用http协议 使用http的编码和解码器
                            channel.pipeline().addLast(new ChunkedWriteHandler());// 以块的方式写
                            channel.pipeline().addLast(new HttpObjectAggregator(65536));// http数据传输是分段的,用HttpObjectAggregator将多段进行聚合
                            // 路径  使用子协议  是否关支持扩展  消息大小  是否携带掩码
                            channel.pipeline().addLast(new WebSocketServerProtocolHandler("/chat",null,true,65535*10,false));//将http协议升级为ws协议
                            // 自定义的Handler
                            channel.pipeline().addLast(new GlobalWebSocketFrameHandler());
                            // 消息 处理器
                            channel.pipeline().addLast(new MessageHandler());
                        }
                    });
            System.out.println("netty 版本 websocket 启动");
            // 绑定端口 启动服务器
            ChannelFuture future = serverBootstrap.bind(port).sync();
            // 关闭通道进行监听
//            future.channel().closeFuture().sync();
            future.channel().closeFuture().addListener((ChannelFuture channelFuture)->{
                System.out.println("通道关闭");
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            });
            System.out.println("xxx");
        }catch(Exception e){
            e.printStackTrace();
        }finally {

        }
    }

}
